package com.qiaofang.applet.common.dto;

import com.qiaofang.applet.common.constant.ResponseCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用响应对象
 * @author QF2019
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataResponse<T> implements Serializable {

    private static final long serialVersionUID = -1;

    @ApiModelProperty(value = "响应码,1表示成功", example = "1")
    private int status;

    @ApiModelProperty(value = "响应消息", example = "成功")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T t;

    /**
     *
     * @return
     */
    public static DataResponse fail(){
        return fail(ResponseCode.FAIL.getMessage());
    }

    /**
     *
     * @param msg
     * @return
     */
    public static DataResponse fail(String msg) {
        return getResult(ResponseCode.FAIL.getStatus(), msg, null);
    }

    /**
     *
     * @param status
     * @param message
     * @return
     */
    public static DataResponse fail(int status, String message) {
        return getResult(status, message, null);
    }

    /**
     *
     * @return
     */
    public static DataResponse ok() {
        return ok(null);
    }

    /**
     *
     * @param data
     * @param <V>
     * @return
     */
    public static <V> DataResponse<V> ok(V data) {
        return getResult(ResponseCode.SUCC.getStatus(), ResponseCode.SUCC.getMessage(), data);
    }

    /**
     *
     * @param status
     * @param message
     * @return
     */
    public static DataResponse ok(int status, String message) {
        return getResult(status, message, null);
    }

    /**
     *
     * @param status
     * @param message
     * @param data
     * @param <V>
     * @return
     */
    public static <V> DataResponse<V> getResult(int status, String message, V data) {
        DataResponse<V> result = new DataResponse<>();
        result.setStatus(status);
        result.setMessage(message);
        result.setT(data);
        return result;
    }
}
